package hr.web.portlet.util.jsf;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
 * 
 * @author ivica
 */
public class JSFPortletUtil {

	public JSFPortletUtil() {
	}

	public static final void sendRedirect(String realPath) {
		try {
			getActionResponse().sendRedirect(realPath);
		} catch (IOException ex) {
			throw new RuntimeException(ex);
		}
	}

	public static final PortletSession getPortletSession(boolean create) {
		Object request = FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		if (request instanceof ActionRequest) {
			return getActionRequest().getPortletSession(create);
		} else {
			return getRenderRequest().getPortletSession(create);
		}
	}

	public static final PortletSession getPortletSession() {
		Object request = FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		if (request instanceof ActionRequest) {
			return getActionRequest().getPortletSession();
		} else {
			return getRenderRequest().getPortletSession();
		}
	}

	public static final ActionResponse getActionResponse() {
		return (ActionResponse) FacesContext.getCurrentInstance()
				.getExternalContext().getResponse();
	}

	public static final ActionRequest getActionRequest() {
		return (ActionRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}

	public static final RenderResponse getRenderResponse() {
		return (RenderResponse) FacesContext.getCurrentInstance()
				.getExternalContext().getResponse();
	}

	public static final RenderRequest getRenderRequest() {
		return (RenderRequest) FacesContext.getCurrentInstance()
				.getExternalContext().getRequest();
	}
}
